package com.ymatou.iapi.couponmanage.testcase;

import java.sql.Date;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponmanage.parameter.QuerySendTask;
import com.ymatou.iapi.couponmanage.parameter.QuerySendTaskBean;
import com.ymatou.iapi.couponmanage.parameter.TaskInfo;
import com.ymatou.iapi.couponmanage.service.QuerySendTaskCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponGenerateTask;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponGenerateTaskWapper;
import com.ymttest.utils.YMTDateUtil;

/**
 * 查询发放任务用例 FreeMaker Template自动生成代码
 * 这个接口查询的平台创建的链接发放优惠券任务
 */
@TestSuite
public class Ts_QuerySendTask {
	private static QuerySendTaskBean querysendtaskBean;
	private static QuerySendTaskCall querysendtaskCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询发放任务用例");
	}

	@Before
	public void caseUp() {
		querysendtaskBean = new QuerySendTaskBean();
		querysendtaskCall = new QuerySendTaskCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(querysendtaskCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QuerySendTask_001() {
		Logger.start(true, "正例-查询发放任务-单条SubTask");
		try {
			YmtCouponGenerateTask task = new YmtCouponGenerateTaskWapper().selectTop1().get(0);
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectByPrimaryKey(task.getCouponBatchId());

			querysendtaskBean.setBatchCode(batch.getsBatchCode());

			Date date1 = new Date(task.getTaskCreateTime().getTime() - 2 * 60 * 60 * 1000);
			Date date2 = new Date(task.getTaskCreateTime().getTime() + 2 * 60 * 60 * 1000);

			querysendtaskBean.setCreateStartTime(YMTDateUtil.getFormatTime(date1, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setCreateEndTime(YMTDateUtil.getFormatTime(date2, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setPageIndex(1);
			querysendtaskBean.setPageSize(1);

			querysendtaskCall.setData(querysendtaskBean);
			querysendtaskCall.callService();

			verifyReturn(task,batch);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QuerySendTask_002() {
		Logger.start(true, "正例-查询发放任务-多条SubTask");
		try {
			Map result = new YmtCouponGenerateTaskWapper().selectMutlipySubTask().get(0);
			
			int taskid = Integer.parseInt(result.get("TaskId").toString());
			int num = Integer.parseInt(result.get("num").toString());
			
			YmtCouponGenerateTask task = new YmtCouponGenerateTaskWapper().selectByPrimaryKey(taskid);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectByPrimaryKey(task.getCouponBatchId());

			querysendtaskBean.setBatchCode(batch.getsBatchCode());

			Date date1 = new Date(task.getTaskCreateTime().getTime() - 2 * 60 * 60 * 1000);
			Date date2 = new Date(task.getTaskCreateTime().getTime() + 2 * 60 * 60 * 1000);

			querysendtaskBean.setCreateStartTime(YMTDateUtil.getFormatTime(date1, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setCreateEndTime(YMTDateUtil.getFormatTime(date2, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setPageIndex(1);
			querysendtaskBean.setPageSize(1);

			querysendtaskCall.setData(querysendtaskBean);
			querysendtaskCall.callService();

			Logger.verifyEquals("0", querysendtaskCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", querysendtaskCall.getMessage(), "验证返回Message");

			QuerySendTask tasks = querysendtaskCall.getQuerySendTask();
			
			Logger.verifyEquals(2, tasks.getRecordCount(), "验证返回RecordCount");
			
			List<TaskInfo> infos = tasks.getTaskList();
			
			TaskInfo info=infos.get(0);
			Logger.verifyEquals(task.getTaskId(), info.getTaskId(), "验证返回code");
			Logger.verifyEquals(task.getCouponBatchId(), info.getBatchId(), "验证返回code");
			Logger.verifyEquals(task.getTaskCreaterId(), info.getCreaterId(), "验证返回code");
			Logger.verifyEquals(task.getTaskCreaterName(), info.getCreaterName(), "验证返回code");
			Logger.verifyEquals(task.getTaskStatus().intValue(), info.getTaskStatus(), "验证返回code");
			Logger.verifyEquals(task.getTaskHadGenerateCount(), info.getCouponSuccessCount(), "验证返回code");
			Logger.verifyEquals(task.getTaskFailGenerateCount(), info.getCouponFailCount(), "验证返回code");
			Logger.verifyEquals(task.getTaskGenerateCount(), info.getCouponTotal(), "验证返回code");
			Logger.verifyEquals(batch.getsBatchCode(), info.getBatchCode(), "验证返回code");
			Logger.verifyEquals(batch.getsCouponName(), info.getBatchName(), "验证返回code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QuerySendTask_003() {
		Logger.start(true, "反例-BatchCode空");
		try {
			Map result = new YmtCouponGenerateTaskWapper().selectMutlipySubTask().get(0);
			
			int taskid = Integer.parseInt(result.get("TaskId").toString());
			int num = Integer.parseInt(result.get("num").toString());
			
			YmtCouponGenerateTask task = new YmtCouponGenerateTaskWapper().selectByPrimaryKey(taskid);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectByPrimaryKey(task.getCouponBatchId());

			querysendtaskBean.setBatchCode(null);

			Date date1 = new Date(task.getTaskCreateTime().getTime() - 2 * 60 * 60 * 1000);
			Date date2 = new Date(task.getTaskCreateTime().getTime() + 2 * 60 * 60 * 1000);

			querysendtaskBean.setCreateStartTime(YMTDateUtil.getFormatTime(date1, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setCreateEndTime(YMTDateUtil.getFormatTime(date2, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setPageIndex(1);
			querysendtaskBean.setPageSize(1);

			querysendtaskCall.setData(querysendtaskBean);
			querysendtaskCall.callService();

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QuerySendTask_004() {
		Logger.start(true, "反例-BatchCode不存在");
		try {
			Map result = new YmtCouponGenerateTaskWapper().selectMutlipySubTask().get(0);
			
			int taskid = Integer.parseInt(result.get("TaskId").toString());
			int num = Integer.parseInt(result.get("num").toString());
			
			YmtCouponGenerateTask task = new YmtCouponGenerateTaskWapper().selectByPrimaryKey(taskid);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectByPrimaryKey(task.getCouponBatchId());

			querysendtaskBean.setBatchCode("99999999");

			Date date1 = new Date(task.getTaskCreateTime().getTime() - 2 * 60 * 60 * 1000);
			Date date2 = new Date(task.getTaskCreateTime().getTime() + 2 * 60 * 60 * 1000);

			querysendtaskBean.setCreateStartTime(YMTDateUtil.getFormatTime(date1, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setCreateEndTime(YMTDateUtil.getFormatTime(date2, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setPageIndex(1);
			querysendtaskBean.setPageSize(1);

			querysendtaskCall.setData(querysendtaskBean);
			querysendtaskCall.callService();

			Logger.verifyEquals("0", querysendtaskCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", querysendtaskCall.getMessage(), "验证返回Message");
			Logger.verifyEquals(0, querysendtaskCall.getQuerySendTask().getTaskList().size(), "验证返回TaskList空");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QuerySendTask_005() {
		Logger.start(true, "反例-时间区间不正确查不出数据");
		try {
			Map result = new YmtCouponGenerateTaskWapper().selectMutlipySubTask().get(0);
			
			int taskid = Integer.parseInt(result.get("TaskId").toString());
			int num = Integer.parseInt(result.get("num").toString());
			
			YmtCouponGenerateTask task = new YmtCouponGenerateTaskWapper().selectByPrimaryKey(taskid);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectByPrimaryKey(task.getCouponBatchId());

			querysendtaskBean.setBatchCode("99999999");

			Date date1 = new Date(task.getTaskCreateTime().getTime() - 4 * 60 * 60 * 1000);
			Date date2 = new Date(task.getTaskCreateTime().getTime() - 2 * 60 * 60 * 1000);

			querysendtaskBean.setCreateStartTime(YMTDateUtil.getFormatTime(date1, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setCreateEndTime(YMTDateUtil.getFormatTime(date2, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setPageIndex(1);
			querysendtaskBean.setPageSize(1);

			querysendtaskCall.setData(querysendtaskBean);
			querysendtaskCall.callService();

			Logger.verifyEquals("0", querysendtaskCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", querysendtaskCall.getMessage(), "验证返回Message");
			Logger.verifyEquals(0, querysendtaskCall.getQuerySendTask().getTaskList().size(), "验证返回TaskList空");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QuerySendTask_006() {
		Logger.start(true, "反例-时间区间CreateStartTime>CreateEndTime不返回数据");
		try {
			Map result = new YmtCouponGenerateTaskWapper().selectMutlipySubTask().get(0);
			
			int taskid = Integer.parseInt(result.get("TaskId").toString());
			int num = Integer.parseInt(result.get("num").toString());
			
			YmtCouponGenerateTask task = new YmtCouponGenerateTaskWapper().selectByPrimaryKey(taskid);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectByPrimaryKey(task.getCouponBatchId());

			querysendtaskBean.setBatchCode("99999999");

			Date date1 = new Date(task.getTaskCreateTime().getTime() - 4 * 60 * 60 * 1000);
			Date date2 = new Date(task.getTaskCreateTime().getTime() - 2 * 60 * 60 * 1000);

			querysendtaskBean.setCreateStartTime(YMTDateUtil.getFormatTime(date2, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setCreateEndTime(YMTDateUtil.getFormatTime(date1, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setPageIndex(1);
			querysendtaskBean.setPageSize(1);

			querysendtaskCall.setData(querysendtaskBean);
			querysendtaskCall.callService();

			Logger.verifyEquals("0", querysendtaskCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", querysendtaskCall.getMessage(), "验证返回Message");
			Logger.verifyEquals(0, querysendtaskCall.getQuerySendTask().getTaskList().size(), "验证返回TaskList空");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private void verifyReturn(YmtCouponGenerateTask task, YmtCouponBatch batch)
	{
		Logger.verifyEquals("0", querysendtaskCall.getCode(), "验证返回code");
		Logger.verifyEquals("操作成功！", querysendtaskCall.getMessage(), "验证返回Message");

		QuerySendTask tasks = querysendtaskCall.getQuerySendTask();
		
		Logger.verifyEquals(1, tasks.getRecordCount(), "验证返回RecordCount");
		
		List<TaskInfo> infos = tasks.getTaskList();
		
		TaskInfo info=infos.get(0);
		Logger.verifyEquals(task.getTaskId(), info.getTaskId(), "验证返回code");
		Logger.verifyEquals(task.getCouponBatchId(), info.getBatchId(), "验证返回code");
		Logger.verifyEquals(task.getTaskCreaterId(), info.getCreaterId(), "验证返回code");
		Logger.verifyEquals(task.getTaskCreaterName(), info.getCreaterName(), "验证返回code");
		Logger.verifyEquals(task.getTaskStatus().intValue(), info.getTaskStatus(), "验证返回code");
		Logger.verifyEquals(task.getTaskHadGenerateCount(), info.getCouponSuccessCount(), "验证返回code");
		Logger.verifyEquals(task.getTaskFailGenerateCount(), info.getCouponFailCount(), "验证返回code");
		Logger.verifyEquals(task.getTaskGenerateCount(), info.getCouponTotal(), "验证返回code");
		Logger.verifyEquals(batch.getsBatchCode(), info.getBatchCode(), "验证返回code");
		Logger.verifyEquals(batch.getsCouponName(), info.getBatchName(), "验证返回code");
	}
}